package com.cy.controller;


import com.cy.common.ResultResponse;
import com.cy.config.RequiresRole;
import com.cy.entity.dto.VerifyDto;
import com.cy.service.impl.UsercoursesServiceImpl;
import com.cy.service.impl.VerifyServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cy.entity.Usercourses;
import com.cy.service.IUsercoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import com.cy.entity.Courses;
import com.cy.entity.Usercourses;
import com.cy.service.IUsercoursesService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  用户课程控制层
 * </p>
 *
 * @author Aleven
 * @since 2024-12-21
 */
@RestController
@RequestMapping("/usercourses")
public class UsercoursesController {
    @Autowired
    private UsercoursesServiceImpl usercoursesService;
    @Autowired
    VerifyServiceImpl verifyService;
    @Operation(summary = "用户选择课程")
    @PostMapping("/chooseCourse")
    @RequiresRole("1")//仅学生
    ResultResponse chooseCourse(@Param("courseId") Integer courseId,HttpServletRequest request) throws Exception{
        VerifyDto vd = verifyService.verifyToken(request);
        return usercoursesService.chooseCourse(courseId,vd.getUserId());
    }

    @Operation(summary =  "根据UserId查询已选课程")
    @GetMapping("/selectCourseRecordByUserId")
    @RequiresRole("1")//仅学生
    ResultResponse selectCourseRecordByUserId(HttpServletRequest request) throws Exception{
        VerifyDto vd = verifyService.verifyToken(request);
        return usercoursesService.selectCourseRecordByUserId(vd.getUserId());
    }

    @Operation(summary =  "退课 即删除记录")
    @DeleteMapping ("/exitCourse")
    @RequiresRole("1")//仅学生
    ResultResponse exitCourse(@Param("UserCourseID") Integer UserCourseID) throws Exception {
        return usercoursesService.exitCourse(UserCourseID);
    }
    @Operation(summary =  "统计某个课程的选课人数")
    @GetMapping("/countUserCourse")
    public ResultResponse countUserCourse(String CourseID) {
        Integer count = usercoursesService.countUsercourses(CourseID);
        return ResultResponse.success(count);
    }



    @Operation(summary = "根据用户兴趣推荐课程")
    @GetMapping("/RecommendByInterest")
    public ResultResponse RecommendByInterest(HttpServletRequest request) throws Exception {
        VerifyDto vd = verifyService.verifyToken(request);

        List<Courses> recommend =usercoursesService.RecommendByInterest(vd.getUserId());
        return ResultResponse.success(recommend,recommend.size());
    }


}
